package com.mes.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.domain.vo.OrderTrackingRecord;
import com.mes.biz.domain.vo.OrderVo;
import com.mes.biz.domain.vo.PieceProductionRecordVo;
import com.mes.biz.domain.vo.ProcessRecordWithOrder;
import com.mes.biz.mapper.BizOrderProcessRecordMapper;
import com.mes.biz.mapper.BizOrderProductMapper;
import com.mes.biz.service.*;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.page.PageDomain;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.exception.BizException;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.sql.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class BizOrderProcessRecordServiceImpl extends BaseServiceImpl<BizOrderProcessRecordMapper, BizOrderProcessRecord> implements BizOrderProcessRecordService {

    @Resource
    private BizOrderProcessRecordMapper bizOrderProcessRecordMapper;
    @Resource
    private BizProductionProcessService bizProductionProcessService;
    @Resource
    private BizProductProcessDetailService bizProductProcessDetailService;
    @Resource
    private BizProductionOrderService bizProductionOrderService;
    @Resource
    private BizOrderService bizOrderService;
    @Resource
    private BizOrderProductMapper bizOrderProductMapper;
    @Resource
    private BizProcessUserService bizProcessUserService;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBizOrderProcessRecord(BizOrderProcessRecord bizOrderProcessRecord) {
        int update = bizOrderProcessRecordMapper.update(bizOrderProcessRecord);
        if(update > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean findInfo(BizProductionOrder bizProductionOrder) {
        Long info = bizOrderProcessRecordMapper.findInfo(bizProductionOrder);
        if (info !=null && info >0){
            return true;
        }
        return false;
    }

//    @Transactional(rollbackFor = Exception.class)
//    public boolean addRecord(BizOrderProcessRecord bizOrderProcessRecord) {
//        if (StringUtils.isEmpty(bizOrderProcessRecord.getProcessPlanId())) {
//            throw new BizException("工序计划id不能为空");
//        }
//        BizProductionProcess bizProductionProcess = bizProductionProcessService.get(bizOrderProcessRecord.getProcessId());
//        if (bizProductionProcess != null) {
//            bizOrderProcessRecord.setTenantId(SecurityUtils.getTenantId());
//            bizOrderProcessRecord.setCompletedVol("1");
//            boolean save = super.save(bizOrderProcessRecord);
//            if (save) {
//                List<String> productIdList = bizOrderProductMapper.getProcessIdByProductId(bizOrderProcessRecord.getProductId(),
//                        bizOrderProcessRecord.getOrderId(), bizOrderProcessRecord.getProcessPlanId());
//                BizOrder bizOrder = bizOrderService.get(bizOrderProcessRecord.getOrderId());
//                if (productIdList !=null && productIdList.size()>0) {
//                    bizOrder.setOrderStatus("5");
//                    bizOrderService.save(bizOrder);
//                }else {
//                    BizOrderProduct bizOrderProduct = bizOrderProductMapper.get(bizOrderProcessRecord.getOrderProductId());
//                    bizOrderProduct.setOperationStatus("1");
//                    bizOrderProductMapper.update(bizOrderProduct);
//                    boolean orderStatus = true;
//                    List<BizOrderProduct> bizOrderProductList = bizOrderProductMapper.getBizOrderProductByOrderId(bizOrderProcessRecord.getOrderId());
//                    if (bizOrderProductList != null && bizOrderProductList.size() > 0) {
//                        for (BizOrderProduct orderProduct : bizOrderProductList) {
//                            if ("0".equals(orderProduct.getOperationStatus())) {
//                                orderStatus = false;
//                                break;
//                            }
//                        }
//                    }
//                    if (orderStatus){
//                        if ("1".equals(bizOrder.getDeliveryType()) && "1".equals(bizOrder.getInstallType())) {
//                            bizOrder.setOrderStatus("9");
//                        } else {
//                            bizOrder.setOrderStatus("6");
//                        }
//                        bizOrderService.save(bizOrder);
//                    }
//                }
//            }
//        } else {
//            throw new BizException("工序不存在");
//        }
//        return true;
//    }

    @Transactional(rollbackFor = Exception.class)
    public boolean addRecord(BizOrderProcessRecord bizOrderProcessRecord,String userId) {
        BizOrderProcessRecord record1 = get(bizOrderProcessRecord.getId());
        boolean flag = bizProcessUserService.findInfoByUserId(userId, record1.getProcessId());
        if (!flag){
            throw new BizException("当前用户没有该工序的权限，请联系管理员配置");
        }
        record1.setCompletedVol("1");
        record1.setCreator(userId);
        record1.setCompletedTime(new Date());
        boolean save = super.save(record1);
        if (save) {
            BizOrderProcessRecord record=new BizOrderProcessRecord();
            record.setOrderId(record1.getOrderId());
            record.setProductId(record1.getProductId());
            record.setCompletedVol("0");
            List<BizOrderProcessRecord> list = mapper.findList(record);
            BizOrder bizOrder = bizOrderService.get(record1.getOrderId());
            if (list != null && list.size() > 0) {
                bizOrder.setOrderStatus("5");
                bizOrder.setWarningStatus("1");
                bizOrderService.save(bizOrder);
            } else {
                BizOrderProduct bizOrderProduct = bizOrderProductMapper.get(record1.getOrderProductId());
                bizOrderProduct.setOperationStatus("1");
                bizOrderProductMapper.update(bizOrderProduct);
                boolean orderStatus = true;
                List<BizOrderProduct> bizOrderProductList = bizOrderProductMapper.getBizOrderProductByOrderId(record1.getOrderId());
                if (bizOrderProductList != null && bizOrderProductList.size() > 0) {
                    for (BizOrderProduct orderProduct : bizOrderProductList) {
                        if ("0".equals(orderProduct.getOperationStatus())) {
                            orderStatus = false;
                            break;
                        }
                    }
                }
                if (orderStatus) {
//                    if ("1".equals(bizOrder.getDeliveryType()) && "1".equals(bizOrder.getInstallType())) {
//                        bizOrder.setOrderStatus("9");
//                    } else {
//                        bizOrder.setOrderStatus("6");
//                    }
                    bizOrder.setOrderStatus("6");
                }
                bizOrder.setWarningStatus("1");
                bizOrderService.save(bizOrder);
            }
        }
        return true;
    }

    @Override
    public List<BizOrderProcessRecord> appletList(BizOrderProcessRecord bizOrderProcessRecord,String userId) {
        List<BizOrderProcessRecord> list = mapper.findList(bizOrderProcessRecord);
        List<BizProcessUser> bizProcessUserList = bizProcessUserService.getListByUserId(userId);
        if(list != null && list.size()>0 && bizProcessUserList !=null && bizProcessUserList.size()>0){
            for (BizOrderProcessRecord record:list){
                record.setFlag(false);
                for (BizProcessUser user:bizProcessUserList){
                    if (record.getProcessId().equals(user.getProcessId())){
                        record.setFlag(true);
                        break;
                    }
                }
            }
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByOrderId(String orderId) {
        return mapper.deleteByOrderId(orderId, BaseEntity.DEL_FLAG_DELETE)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteByOrderProductId(String orderProductId) {
        return mapper.deleteByOrderProductId(orderProductId, BaseEntity.DEL_FLAG_DELETE)>0;
    }

//    @Override
//    public PageInfo<ProcessRecordWithOrder> pieceProductionList(BizOrderProcessRecord bizOrderProcessRecord) {
//        PageDomain page = bizOrderProcessRecord.getPage();
//        Integer pageNum = page.getPageNum();
//        Integer pageSize = page.getPageSize();
//        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
//        {
//            String orderBy = SqlUtil.escapeOrderBySql(page.getOrderBy());
//            Boolean reasonable = page.getReasonable();
//            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
//        }
//        return new PageInfo(mapper.pieceProductionList(bizOrderProcessRecord));
//    }
//
//    @Override
//    public List<ProcessRecordWithOrder> exportList(BizOrderProcessRecord bizOrderProcessRecord) {
//
//        return mapper.pieceProductionList(bizOrderProcessRecord);
//    }

    @Override
    public List<BizOrderProcessRecord> getByOrderId(String orderId) {
        return  mapper.getByOrderId(orderId);
    }

    @Override
    public List<OrderTrackingRecord> findCompletedByOrderProductId(String orderProductId,String creator) {
        return mapper.findCompletedByOrderProductId(orderProductId,creator);
    }

    @Override
    public List<PieceProductionRecordVo> findPieceProductionDetailList(String orderProductId, String creator) {
        return mapper.findPieceProductionDetailList(orderProductId,creator);
    }

    @Override
    public List<BizOrderProcessRecord> findListByOrderId(BizOrderProcessRecord bizOrderProcessRecord) {
        return mapper.findListByOrderId(bizOrderProcessRecord);
    }
}
